//
//  CheckingViewController.m
//  Frame
//
//  Created by 这是工作本 on 16/1/21.
//  Copyright © 2016年 这是工作本. All rights reserved.
//

#import "CheckingViewController.h"

@interface CheckingViewController () <UIGestureRecognizerDelegate, UITextFieldDelegate>

@property (nonatomic,retain) UITextField *input;

@property (nonatomic,retain) UITextField *password;

@property (nonatomic,retain) UITextField *check;

@property (nonatomic,retain) UIButton *next;

@property (nonatomic,retain) UIButton *get;

@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, assign) NSInteger seconds;
@end

@implementation CheckingViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.password resignFirstResponder];
    [self.input resignFirstResponder];
    [self.check resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    
    //隐藏自动返回
    self.navigationItem.hidesBackButton = YES;
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"矩形-3@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    //设置title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0],NSForegroundColorAttributeName,nil]];
    
    
    //背景
    
    UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
    back.image = [UIImage imageNamed:@"loging.png"];
    [self.view addSubview:back];

    //请输入验证码
    self.input = [[UITextField alloc] initWithFrame:CGRectMake(20 * ScreenWidth , 40 * ScreenHeigth,  220 * ScreenWidth, 45 * ScreenHeigth)];
    self.input.clearButtonMode = UITextFieldViewModeAlways;
    self.input.clearsOnBeginEditing = YES;
    self.input.layer.cornerRadius = 5 * ScreenWidth;
    self.input.layer.masksToBounds = YES;
    self.input.delegate = self;
    self.input.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
    self.input.keyboardType = UIKeyboardTypePhonePad;
    self.input.placeholder = @"请输入验证码";
    self.input.textColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1.0];
    [self.view addSubview:self.input];
    //设置字体距离左边框的距离
    self.input.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10 * ScreenWidth, 0)];
    self.input.leftViewMode = UITextFieldViewModeAlways;
    
   //获取验证码
    self.get = [UIButton buttonWithType:UIButtonTypeCustom];
    self.get.backgroundColor = [UIColor colorWithRed:249 / 255.0 green:182 / 255.0 blue:21 / 255.0 alpha:1.0];
    self.get.layer.cornerRadius = 5 * ScreenWidth;
    self.get.layer.masksToBounds = YES;
    self.get.frame = CGRectMake(self.input.frame.origin.x + self.input.frame.size.width + 20 * ScreenWidth, self.input.frame.origin.y, 100 * ScreenWidth, 45 * ScreenHeigth);
    [self.get setTitleColor:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.get.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.get setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.get addTarget:self action:@selector(getAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.get];
    self.seconds = 61;
    
    
    //输入密码
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(self.input.frame.origin.x , self.input.frame.origin.y + self.input.frame.size.height + 20 * ScreenHeigth,  [UIScreen mainScreen].bounds.size.width - 40 * ScreenWidth, 45 * ScreenHeigth)];
    self.password.clearButtonMode = UITextFieldViewModeAlways;
    self.password.clearsOnBeginEditing = YES;
    self.password.layer.cornerRadius = 5 * ScreenWidth;
    self.password.layer.masksToBounds = YES;
    self.password.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
//    self.password.keyboardType = UIKeyboardTypeNamePhonePad;
    self.password.placeholder = @"确认密码(6-16位)";
    [self.view addSubview:self.password];
    //设置字体距离左边框的距离
    self.password.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10 * ScreenWidth, 0)];
    self.password.leftViewMode = UITextFieldViewModeAlways;

    
    //确认密码
    self.check = [[UITextField alloc] initWithFrame:CGRectMake(20 * ScreenWidth , self.password.frame.origin.y + self.password.frame.size.height + 20 * ScreenHeigth,  [UIScreen mainScreen].bounds.size.width - 40 * ScreenWidth, 45 * ScreenHeigth)];
    self.check.clearButtonMode = UITextFieldViewModeAlways;
    self.check.clearsOnBeginEditing = YES;
    self.check.layer.cornerRadius = 5 * ScreenWidth;
    self.check.layer.masksToBounds = YES;
    self.check.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
//    self.check.keyboardType = UIKeyboardTypeNamePhonePad;
    self.check.placeholder = @"确认密码";
    [self.view addSubview:self.check];
    //设置字体距离左边框的距离
    self.check.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10 * ScreenWidth, 0)];
    self.check.leftViewMode = UITextFieldViewModeAlways;


    //下一步
    self.next = [UIButton buttonWithType:UIButtonTypeCustom];
    self.next.backgroundColor = [UIColor colorWithRed:249 / 255.0 green:182 / 255.0 blue:21 / 255.0 alpha:1.0];
    self.next.layer.cornerRadius = 5 * ScreenWidth;
    self.next.layer.masksToBounds = YES;
   self.next.frame = CGRectMake(self.input.frame.origin.x, self.check.frame.origin.y + self.check.frame.size.height + 20 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width - 40 * ScreenWidth, 45 * ScreenHeigth);
    [self.next setTitleColor:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.next.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.next setTitle:@"下一步" forState:UIControlStateNormal];
    [self.next addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.next];

    //添加全屏右滑动手势
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

    
    //点击空白回收键盘
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction)];
    [self.view addGestureRecognizer:ges];
    
    
    
}

//限制输入字数

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (self.input == textField)  //判断是否时我们想要限定的那个输入框
    {
        if (toBeString.length > 6) { //如果输入框内容大于20则弹出警告
           
            
            return NO;
            
        }
    }

    
    
    return YES;
    
}


//回收键盘
- (void)gesAction
{
    [self.password resignFirstResponder];
    [self.input resignFirstResponder];
    [self.check resignFirstResponder];
    
    
}


//倒计时方法验证码实现倒计时60秒，60秒后按钮变换开始的样子
-(void)timerFireMethod:(NSTimer *)theTimer {
    if (_seconds == 1) {
        [theTimer invalidate];
        self.seconds = 60;
        [self.get setTitle:@"获取验证码" forState: UIControlStateNormal];
        [self.get setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.get setEnabled:YES];
    }else{
        self.seconds--;
        NSString *title = [NSString stringWithFormat:@"%ld",self.seconds];
        [self.get setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.get setEnabled:NO];
        [self.get setTitle:title forState:UIControlStateNormal];
    }
}

//如果登陆成功，停止验证码的倒数，
- (void)releaseTImer {
    if (_timer) {
        if ([self.timer respondsToSelector:@selector(isValid)]) {
            if ([_timer isValid]) {
                [_timer invalidate];
                self.seconds = 60;
            }
        }
    }
}


- (void)getAction:(UIButton *)sender
{
    //计时器
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
   

}


- (void)nextAction:(UIButton *)sender
{
    
    
    
}

- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
